package com.sskj.cndragon.ui.fragment;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.OrderBuyListBean;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.presenter.BuyInFragmentPresenter;
import com.sskj.cndragon.ui.activity.OrderBuyDetailActivity;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;


public class BuyInFragment extends BaseFragment<BuyInFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter<OrderBuyListBean.OrderBuyBean> slimAdapter;
    private MVCCoolHelper<List<OrderBuyListBean.OrderBuyBean>> mvcHelper;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_buy_in;
    }

    @Override
    public BuyInFragmentPresenter getPresenter() {
        return new BuyInFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void initView() {
        super.initView();
        recyclerView.setLayoutManager(new LinearLayoutManager(App.INSTANCE));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(App.INSTANCE)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_buy_in, new SlimInjector<OrderBuyListBean.OrderBuyBean>() {
            @Override
            public void onInject(OrderBuyListBean.OrderBuyBean data, IViewInjector injector, List payloads) {
                injector.clicked(R.id.constraint, v -> OrderBuyDetailActivity.start(App.INSTANCE, data.getId()))
                        .text(R.id.tv_order, data.getSwiftNo())
                        .text(R.id.tv_status, data.getStatus() == 1 ? App.INSTANCE.getString(R.string.appDaifukuan) :
                                data.getStatus() == 2 ? App.INSTANCE.getString(R.string.appFukaunzhong) :
                                        data.getStatus() == 3 ? App.INSTANCE.getString(R.string.appFukuanchenggong) :
                                                data.getStatus() == 4 ? App.INSTANCE.getString(R.string.appYiwancheng) :
                                                        data.getStatus() == 5 ? App.INSTANCE.getString(R.string.appShensuzhong) :
                                                                data.getStatus() == 6 ? App.INSTANCE.getString(R.string.appYiquxiao) : "")
                        .text(R.id.tv_type, data.getName())
                        .text(R.id.tv_price, "￥" + NumberUtil.keep2(data.getPrice()))
                        .text(R.id.tv_days, data.getContractDays())
                        .text(R.id.tv_incomes, NumberUtil.keepNoZero(data.getProfitRatio()) + "%")
                        .text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<OrderBuyListBean.OrderBuyBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<OrderBuyListBean.OrderBuyBean>)
                        page -> mPresenter.getOrderBuyRecord(SPUtil.get(SPConfig.ID, ""), page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public static BuyInFragment newInstance() {
        BuyInFragment fragment = new BuyInFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }
}
